/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.paint.classic;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Picture;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.Word;
import chabernac.android.wordbattle.paint.AbstractLettersDrawable;

public class WordPictureDrawable extends AbstractLettersDrawable {
  private final int myColor;
  private final Paint myPaint = new Paint();
  private float myMinSize;

  private Picture myPicture = null;
  private WordBattleView myWordBattleView;

  public WordPictureDrawable( Word aWord, int aColor ) {
    super(aWord);
    myColor = aColor;

    init();
  }

  private void init(){
    myPaint.setColor( myColor );
    myPaint.setStyle(Style.FILL);
    myPaint.setAntiAlias(true);
  }

  @Override
  public void paint( Canvas aCanvas, View aView ) {
    myWordBattleView = (WordBattleView)aView;
    myMinSize = (float)(Math.min(myWordBattleView.getHeightPerRow(), myWordBattleView.getWidthPerColumn()) * 0.7 );
    myPaint.setStrokeWidth( myMinSize );
    
    if(myWord.getLetters().isEmpty()) return;

    if(myPicture == null){
      myPaint.setAlpha( 180 );
//      myPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
//      myPaint.setXfermode(new AvoidXfermode(myColor, 100, Mode.AVOID));
      
      myPicture = new Picture();
      Canvas theCanvas = myPicture.beginRecording(myWordBattleView.getWidth(), myWordBattleView.getHeight());
      
      float[] thePoints = new float[(myWord.getEndPointLetters().size() - 1) * 4];

      Letter thePreviousLetter = null;
      float thePreviousCenterX = -1;
      float thePreviousCenterY = -1;
      for(int i=0;i<myWord.getEndPointLetters().size();i++){
        Letter theLetter = myWord.getEndPointLetters().get(i);

        float theCenterX = (float)(WordBattleView.BORDER_SIZE + (theLetter.getColumn() + 0.5) * myWordBattleView.getWidthPerColumn());
        float theCenterY = (float)(WordBattleView.BORDER_SIZE + (theLetter.getRow() + 0.5) * myWordBattleView.getHeightPerRow());

        if(i > 0 && i < myWord.getEndPointLetters().size() - 1){
          theCanvas.drawCircle(theCenterX, theCenterY, myMinSize / 2, myPaint);
        }

        if(thePreviousLetter != null){
//          theCanvas.drawLine(thePreviousCenterX, thePreviousCenterY, theCenterX, theCenterY, myPaint);
          int j = i - 1;
          thePoints[j*4] = thePreviousCenterX;
          thePoints[j*4 + 1] = thePreviousCenterY;
          thePoints[j*4 + 2] = theCenterX;
          thePoints[j*4 + 3] = theCenterY;
        }

        thePreviousLetter = theLetter;
        thePreviousCenterX = theCenterX;
        thePreviousCenterY = theCenterY;
      }
      modifyFirstAndLastPoint(myWordBattleView, thePoints);
      theCanvas.drawLines(thePoints, myPaint);
    }

    myPaint.setAlpha( 100 );
    aCanvas.drawPicture(myPicture);
  }

}
